package com.songcha.library_business.handler

import android.content.Context
import com.songcha.library_business.BuildConfig
import com.songcha.library_business.bean.book.BookNetBean
import com.songcha.library_business.ui.dialog.HomeBookRecommendDialog
import com.songcha.library_common.helper.SharedPreferenceHelper
import com.songcha.library_common.util.TimeUtil
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit

class HomeBookRecommendDialogHandler {
    companion object{

        private var mShowHomeBookRecommendDialogDispose: Disposable? = null
        private var mIsInterruptHomeBookRecommendDialog = false
        private var mIsShowHomeBookRecommendDialogSuccess = false
        private var mShowHomeBookRecommendDialogStartTime = 0L

        @JvmStatic
        private fun showHomeBookRecommendDialog(context: Context,onGetBook:((BookNetBean)->Unit)->Unit){
            onGetBook{book->
                val dialog = HomeBookRecommendDialog(context,book)
                dialog.show()
                mIsShowHomeBookRecommendDialogSuccess = true
                val startTime = TimeUtil.getCurrentTimeSecond()
                SharedPreferenceHelper.setLong("home_book_recommend","show_dialog_start_time", startTime)
                mShowHomeBookRecommendDialogStartTime = startTime
            }

        }

        @JvmStatic
        fun handleShowHomeBookRecommendDialog(context: Context, onGetBook:((BookNetBean)->Unit)->Unit){
            if(mIsShowHomeBookRecommendDialogSuccess) return
            if(mShowHomeBookRecommendDialogStartTime == 0L)
                mShowHomeBookRecommendDialogStartTime = SharedPreferenceHelper.getLong("home_book_recommend","show_dialog_start_time")
            val todayTime = TimeUtil.getTodayZeroTimeStamp()

            if(mShowHomeBookRecommendDialogStartTime >= todayTime && mShowHomeBookRecommendDialogStartTime < todayTime + 86400){
                if(!BuildConfig.DEBUG)
                    return
            }
            if(mIsInterruptHomeBookRecommendDialog && !mIsShowHomeBookRecommendDialogSuccess){
                showHomeBookRecommendDialog(context,onGetBook)
                return
            }

            val countDown = 5L
            mShowHomeBookRecommendDialogDispose = Observable.intervalRange(0,countDown,0,1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe { it->
                    if(it == countDown - 1){
                        showHomeBookRecommendDialog(context,onGetBook)
                    }
                }
        }

        @JvmStatic
        fun handleInterruptHomeBookRecommendDialog(){
            mShowHomeBookRecommendDialogDispose?.dispose()
            if(!mIsShowHomeBookRecommendDialogSuccess)
                mIsInterruptHomeBookRecommendDialog = true
        }
    }
}